package com.hanxiaozhang.oauth2.controller;

import com.hanxiaozhang.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述: <br>
 * 〈〉
 *
 * @Author:hanxinghua
 * @Date: 2020/7/27
 */
@RestController
public class BaseController {

    @Autowired
    private HttpServletRequest request;

    /**
     * 获取用户id
     *
     * @return
     */
    protected Long getUserId() {
        String userId = request.getHeader(Constant.USER_ID);
        if (userId == null) {
            return null;
        }
        return Long.valueOf(userId);
    }

    /**
     * 获取用户名称
     *
     * @return
     */
    protected String getUserName() {
        return request.getHeader(Constant.USER_NAME);
    }

    /**
     * 获取所有Headers
     *
     * @return
     */
    protected Map<String, String> listHeadersInfo() {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }

}
